/*--------------------------------------------------------------------------------------*\
**
** adv_ferry.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adventure_events_spells_effect.h"

#include "adventure_map.h"
#include "adventure_object.h"
#include "adventure_object_ptr.h"
#include "adventure_spell_effect.h"
#include "army.h"
#include "combat_sounds.h"
#include "compressed_filter.h"
#include "game_time.h"
#include "music.h"
#include "saved_game_header.h"
#include "sound.h"
#include "sound_cache.h"

namespace 
{
	const int k_memory_event_buffer_size	= 0;
	const int k_event_lifespan				= 0*1000;			//in mili-seconds

	// -----------------------------------------------------------------
	// -----------------------------------------------------------------
	void play_sound( t_spell spell )
	{
		assert( spell >= 0 && spell < k_spell_count );
		t_sound_ptr sound_ptr = get_combat_sound( spell );
		if ( sound_ptr.get() != 0 )
			sound_ptr->play( get_sound_volume() );
	}
};

t_adventure_event_spells_effect::t_adventure_event_spells_effect ( ) 
: t_adventure_event_base ( k_create_adv_spell_effects ) 
{ 	
	m_target_global_id		= k_invalid_global_id;
	m_spell_id				= k_invalid_spell_effect;
	m_run_modal				= false;
}

t_adventure_event_spells_effect::t_adventure_event_spells_effect ( t_adventure_global_id target_global_id, k_spell_effects_event_id spell_id, bool run_modal ) 
: t_adventure_event_base ( k_create_adv_spell_effects ) 
{ 
	m_target_global_id		= target_global_id;
	m_spell_id				= spell_id;
	m_run_modal				= run_modal;
	
	assert( m_target_global_id != k_invalid_global_id && m_spell_id != k_invalid_spell_effect );
	
	if (m_target_global_id == k_invalid_global_id || m_spell_id == k_invalid_spell_effect)
		m_is_valid = false;
}

void t_adventure_event_spells_effect::execute_event( t_adventure_map* map, t_saved_game_header const & header ) 
{
	t_adventure_object * object = map->get_adventure_object_with_gid ( m_target_global_id );
	
	if (object == NULL)
		return;

	t_army* dyn_cast_object = dynamic_cast<t_army*>(object);
	
	if (dyn_cast_object == NULL) 
		return;
	
	switch (m_spell_id)
	{
	case k_spell_magic_resistance_event_id:
		{
			t_counted_ptr< t_adv_magic_resistance_effect > effect_ptr( new t_adv_magic_resistance_effect( *dyn_cast_object ) );
			play_sound( k_spell_magic_resistance );
			
			if (m_run_modal == true)
				effect_ptr->run_modal();
		}
		break;
	case k_spell_mire_event_id:	
		{
			t_counted_ptr< t_adv_mire_effect > effect_ptr( new t_adv_mire_effect( *dyn_cast_object ) );
			play_sound( k_spell_mire );
			
			if (m_run_modal == true)
				effect_ptr->run_modal();
		}
		break;
	case k_invalid_spell_effect: default:
		{
			m_run_modal = false;
		}
		break;
	}
	
	m_time_end = get_time() + k_event_lifespan;
	
	return;
}

void t_adventure_event_spells_effect::undo_event( t_adventure_map* map , t_saved_game_header const & header )
{

}

bool t_adventure_event_spells_effect::read( std::streambuf & buffer , int version )
{
	m_target_global_id		= read_adventure_global_id(buffer);
	m_spell_id				= 	get<int> ( buffer );
	m_run_modal				= 	get<bool> ( buffer );

	return t_adventure_event_base::read( buffer , version );
}

bool t_adventure_event_spells_effect::write( std::streambuf & buffer )
{
	write_adventure_global_id(buffer, m_target_global_id);
	put<int>	(buffer, m_spell_id);
	put<bool>	(buffer, m_run_modal);
	
	return t_adventure_event_base::write( buffer );
}
